package org.wanda.pv.controller.basic;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.framework.page.Pagination;
import org.wanda.pv.model.basic.PointClass;
import org.wanda.pv.service.basic.PointClassService;

import java.util.List;

/**
 * @author LiuXuyang
 * Created by lxy on 2018/5/10.
 */

@RestController
@RequestMapping("/basic/pointClass")
public class PointClassController {
    private final static Logger log = LoggerFactory.getLogger( PointClassController.class);

    @Autowired
    Dao dao;

    @Autowired
    private PointClassService pointClassService;
    /**
     * @api {post} /basic/problemTypes/add 添加要点类型
     * @apiParam {String} name 问题类型名称
     * @apiGroup 问题类型
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(PointClass pointClass){
        try{
            Result result=pointClassService.add(pointClass);
            return result;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("fali","服务器内部错误！");
        }
    }

    /**
     * @api {post} /basic/problemTypes/del 删除问题类型
     * @apiParam {String} id 问题类型ID
     * @apiGroup 问题类型
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public Object del(@RequestParam("id")Long id){
        try{

            Result result=pointClassService.del(id);
            return result;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("fali","服务器内部错误！");
        }
    }

    /**
     * @api {post} /basic/problemTypes/update 修改问题类型
     * @apiParam {String}id 问题类型ID
     * @apiParam {String} name 问题类型名称
     * @apiGroup 问题类型
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(PointClass pointClass){
        try{
            Result result=pointClassService.updatePointClass(pointClass);
            return result;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("fali","服务器内部错误！");
        }
    }


    /**
     * @api {post} /basic/problemTypes/list 分页查询所有问题类型
     * @apiParam {String} pageNum 当前页码(默认值为1)
     * @apiParam {String} pageSize 单页显示条数(默认值为10)
     * @apiParam {String} name 问题类型名称(默认值为空)
     * @apiGroup 问题类型
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "name", defaultValue = "") String name) {
        try {
            Cnd cnd = Cnd.where("name", "like", "%" + name + "%");
            Pagination listPage = pointClassService.listPage(pageNum, pageSize, cnd
                    .and("delFlag", "=", "0"));
            List<PointClass> list = (List<PointClass>) listPage.getList();

            return Result.success("success", listPage);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("fail", "服务器内部错误!");
        }

    }


    /**
     * @api {post} /basic/problemTypes/getAll 获取所有问题类型
     * @apiParam {String} name 问题类型名称(默认值为空)
     * @apiGroup 问题类型
     */
    @RequestMapping(value = "/getAll",method = RequestMethod.POST)
    public Object getAll( @RequestParam(value = "name",defaultValue = "")String name){
        try{
            List<PointClass> listPage = pointClassService.query(Cnd.where("name", "like", "%" + name + "%")
                    .and("delFlag","=","0"));
            return Result.success("success",listPage);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("fail","服务器内部错误!");
        }

    }
}
